#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

const int N = 2e5 + 5;

int T, n;
ll a[N];

namespace P20{
    ll Get(int st){
        ll res = 0;
        for(int i = 0; i < n; i++){
            for(int j = i - 1; j >= 0; j--){
                if(((st >> i) & 1) == ((st >> j) & 1)){
                    if(a[i + 1] == a[j + 1]) res += a[i + 1];
                    break;
                }
            }
        }
        return res;
    }

    ll Solve(){
        ll ans = 0;
        for(int st = 0; st < (1 << n); st++){
            ans = max(ans, Get(st));
        }

        return ans;
    }
}

namespace P50{
    ll f[N], sum[N];
    int g[N], l[N];

    int Find(int x){
        if(x <= 0) return x - 1;
        if(g[x] < x - 1) return x - 1;
        return Find(g[x]);
    }

    void Init(){
        for(int i = 1; i < n; i++){
            sum[i] = sum[i - 1];
            if(a[i] == a[i + 1]){
                sum[i] += a[i];
            }
        }
    }

    ll Get(int j, int i){
        int pos;
        if(l[j] >= 0) pos = l[j];
        else{
            pos = Find(j);
            l[j] = pos;
        }
        ll res = 0;

        if(a[j + 1] == a[pos]) res += a[pos];
        if(a[j] == a[i]) res += a[i];

        if(i - 2 >= j) res += sum[i - 2] - sum[j];

        return res;
    }

    ll Solve(){
        for(int i = 0; i <= n + 5; i++){
            f[i] = 0;
            g[i] = 0;
            l[i] = -1;
            sum[i] = 0;
        }
        Init();

        ll ans = 0;

        for(int i = 1; i <= n; i++){
            ll tmp = 0, cur = 0, pos = 0;
            for(int j = i - 1; j >= 0; j--){
                cur = Get(j, i) + f[j];

                if(cur > tmp){
                    tmp = cur;
                    pos = j;
                }
            }

            f[i] = tmp;
            g[i] = pos;
            ans = max(ans, f[i]);
        }

        return ans;
    }
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);

    freopen("color.in", "r", stdin);
    freopen("color.out", "w", stdout);

    cin >> T;
    while(T--){
        cin >> n;
        for(int i = 1; i <= n; i++){
            cin >> a[i];
        }

        if(n <= 15) cout << P20::Solve() << '\n';
        else cout << P50::Solve() << '\n';
    }

    return 0;
}
